#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <signal.h>

#ifndef TIOCGWINSZ
#include <sys/ioctl.h>
#endif

static void pr_winsize( int fd )
{
	struct winsize size;

	if( ioctl( fd, TIOCGWINSZ, (char *)&size ) < 0 ) {
		fprintf( stderr, "TIOCGWINSZ error\n" );
		exit( 0 );
	}

	printf( "%d rows, %d columns\n", size.ws_row, size.ws_col );
}

static void sig_winch( int signo )
{
	printf( "SIGWINCH %d received.\n", signo );
	pr_winsize( STDIN_FILENO );
}


int main( int argc, char *argv[] )
{

	// if not a terminal, exit
	if( !isatty( STDIN_FILENO ) ) {
		fprintf( stderr, "standard input is not a terminal device.\n" );
		exit( 0 );
	}

	if( signal( SIGWINCH, sig_winch ) == SIG_ERR ) {
		fprintf( stderr, "signal, error.\n" );
		exit( 0 );
	}

	pr_winsize( STDIN_FILENO );
	while( true )
		pause();

	return 0;
}
